package com.ynet.hmds28.user.mq;


import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.openservices.ons.api.Action;
import com.aliyun.openservices.ons.api.ConsumeContext;
import com.aliyun.openservices.ons.api.Message;
import com.aliyun.openservices.ons.api.MessageListener;
import com.taobao.middleware.logger.Logger;
import com.taobao.middleware.logger.LoggerFactory;
import com.ynet.hm.merchant.domain.ContentBean;
import com.ynet.hm.user.domain.ServiceConsumeBean;
import com.ynet.hm.user.service.StatisticalBillService;

public class AliMQConsumerListener implements MessageListener {
    private static final Logger logger = LoggerFactory.getLogger(AliMQConsumerListener.class);

    @Autowired
    private StatisticalBillService statisticalBillService;
    
    @Override
    public Action consume(Message message, ConsumeContext context) {
        String msg = "";
        try {
            //do something..
            msg = new String(message.getBody(), "UTF-8");
            logger.info("订阅消息：" + msg);
            ContentBean ab = JSONObject.parseObject(msg, ContentBean.class);
            ServiceConsumeBean result = statisticalBillService.statisticalBill(ab.getProvideId(), ab.getMerchantNo());
            logger.info("统计扣费成功，第"+result.getCallnum()+"次调用服务，累计计费"+result.getCallcost()+"元。");
            return Action.CommitMessage;
        } catch (Exception e) {
            //消费失败
            logger.info("消费失败：" + msg);
            return Action.ReconsumeLater;
        }
    }
}
